package idu0080.veebitoorik.serviceclient;

import java.util.List;

import idu0080.yl6_OpenERPServices_client.BusinessLogicException_Exception;
import idu0080.yl6_OpenERPServices_client.Customer;
import idu0080.yl6_OpenERPServices_client.CustomerService_CustomerServiceImplPort_Client;
import idu0080.yl6_OpenERPServices_client.DeliveryService_DeliveryServiceImplPort_Client;
import idu0080.yl6_OpenERPServices_client.Invoice;
import idu0080.yl6_OpenERPServices_client.InvoiceService_InvoiceServiceImplPort_Client;
import idu0080.yl6_OpenERPServices_client.PaymentService_PaymentServiceImplPort_Client;
import idu0080.yl6_OpenERPServices_client.Product;
import idu0080.yl6_OpenERPServices_client.ProductService_ProductServiceImplPort_Client;

public class SOAPClient 
{
	
	private String errorMessage = "";
	private String errorCode = "";


	public SOAPClient() 
	{
		// TODO Auto-generated constructor stub
	}
	
	public Product getProductById(int productId)
	{
		Product product = null;
		ProductService_ProductServiceImplPort_Client productService = new ProductService_ProductServiceImplPort_Client();
		product = productService.getProductById(productId);
		
		return product;
	}
	
	public List<Product> searchProductsByName(String productName)
	{
		List<Product> product = null;
		ProductService_ProductServiceImplPort_Client productService = new ProductService_ProductServiceImplPort_Client();
		product = productService.getProductsByName(productName);
		
		return product;
	}
	
	public Customer searchCustomerByName(String customerName)
	{
		Customer customer = null;
		
		CustomerService_CustomerServiceImplPort_Client customerService = new CustomerService_CustomerServiceImplPort_Client();
		customer = customerService.searchCustomerByName(customerName);
		
		return customer;
	}
	
	public Invoice createInvoice(Customer customer, String comment, Product product, Double productPrice, Double quantityToOrder)
	{
		Invoice invoiceCreated = null;
		
		InvoiceService_InvoiceServiceImplPort_Client invoiceService = new InvoiceService_InvoiceServiceImplPort_Client();
		invoiceCreated = invoiceService.createInvoice(customer, comment, product, productPrice, quantityToOrder);
		
		return invoiceCreated;
	}
	
	public String createInvoiceAndReceiveInvoiceNumber(Customer customer, String comment, Product product, Double productPrice, Double quantityToOrder)
	{
		String invoiceNumberToReturn = "";
		Invoice invoiceCreated = null;
		
		InvoiceService_InvoiceServiceImplPort_Client invoiceService = new InvoiceService_InvoiceServiceImplPort_Client();
		invoiceCreated = invoiceService.createInvoice(customer, comment, product, productPrice, quantityToOrder);
		invoiceNumberToReturn = invoiceCreated.getNumber();
		
		return invoiceNumberToReturn;
	}
	
	public Invoice getInvoiceByNumber(String invoiceNumber)
	{
		Invoice invoice = null;
		
		InvoiceService_InvoiceServiceImplPort_Client invoiceService = new InvoiceService_InvoiceServiceImplPort_Client();
		invoice = invoiceService.getInvoiceByNumber(invoiceNumber);
		
		return invoice;
	}
	
	public String makePayment(String invoiceNumber, int customerId, Double paydAmount)
	{
		String paymentNumber = "";
		PaymentService_PaymentServiceImplPort_Client paymentService = new PaymentService_PaymentServiceImplPort_Client();
		
		try 
		{
			paymentNumber = paymentService.makePayment(invoiceNumber, customerId, paydAmount);
		} 
		catch (BusinessLogicException_Exception e) 
		{
			this.errorMessage = e.getFaultInfo().getErrorMsg();
		    this.errorCode = e.getMessage();
		    paymentNumber = Integer.toString(-1);
		}
		
		return paymentNumber;
	}
	
	public int registerProductDelivery(String invoiceNumber, Double quantityToGiveOut)
	{
		int deliveryRegistrationId = -1;
		
		DeliveryService_DeliveryServiceImplPort_Client deliveryService = new DeliveryService_DeliveryServiceImplPort_Client();
		try 
		{
			deliveryRegistrationId = deliveryService.registerProductDelivery(invoiceNumber, quantityToGiveOut);
		} 
		catch (BusinessLogicException_Exception e) 
		{
			this.errorMessage = e.getFaultInfo().getErrorMsg();
		    this.errorCode = e.getMessage();
		    deliveryRegistrationId = -1;
		}
		
		return deliveryRegistrationId;
	}
	
	
	public String getErrorMessage() 
	{
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) 
	{
		this.errorMessage = errorMessage;
	}

	public String getErrorCode()
	{
		return errorCode;
	}

	public void setErrorCode(String errorCode) 
	{
		this.errorCode = errorCode;
	}
}
